﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Implementation_Main
{
    public static string line = new string('-', 50);

    static void Main(string[] args)
    {
        string[] states =
        {
            "q0",
            "q1",
            "q2",
            "q3",
        };

        string[] transitions = 
        {
            "q0;q0;0",
            "q0;q0;1",
            "q0;q1;1",
            "q1;q2;0",
            "q1;q2;1",
            "q2;q3;0",
            "q2;q3;1",
        };

        string startState = "q0";
        string[] finalStates = { "q3" };

        FiniteStateMachine nonDeterministicMachine = new FiniteStateMachine();

        //Adding the states
        foreach (string state in states)
        {
            nonDeterministicMachine.AddState(state);
        }

        //Adding the transitions
        string[] transitionArguments;
        string from;
        string to;
        string value;
        foreach (string transition in transitions)
        {
            transitionArguments = ExtractTransitionArguments(transition);

            from = transitionArguments[0];
            to = transitionArguments[1];
            value = transitionArguments[2];
            if (value == "epsilon")
            {
                nonDeterministicMachine.AddTransition(from, to, FiniteStateMachine.Epsilon);
            }
            else
            {
                nonDeterministicMachine.AddTransition(from, to, value);
            }
        }

        //Setting the StartState
        nonDeterministicMachine.SetStartState(startState);


        //Setting the FinalStates
        foreach (string finalState in finalStates)
        {
            nonDeterministicMachine.AddStateToFinalStates(finalState);
        }

        Console.WriteLine(nonDeterministicMachine.ToDeterministic().Count);
        Console.WriteLine(nonDeterministicMachine.ToDeterministic().ToFullString());
    }

    public static string[] ExtractTransitionArguments(string transition)
    {
        List<string> arguments = new List<string>();
        StringBuilder currentArgument = new StringBuilder();
        char currentCharacter;
        for (int i = 0; i < transition.Length; i++)
        {
            currentCharacter = transition[i];
            if (currentCharacter == ';')
            {
                arguments.Add(currentArgument.ToString());
                currentArgument.Clear();
                continue;
            }
            currentArgument.Append(currentCharacter);
        }
        if (currentArgument.Length > 0)
        {
            arguments.Add(currentArgument.ToString());
        }

        return arguments.ToArray();
    }
}